package com.lxmes.hr.service.impl;

import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.domain.NcRewardPunishment;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcSchedulingRulesMapper;
import com.lxmes.hr.domain.NcSchedulingRules;
import com.lxmes.hr.service.INcSchedulingRulesService;

import javax.validation.Validator;

/**
 * 排班规则Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-25
 */
@Service
public class NcSchedulingRulesServiceImpl implements INcSchedulingRulesService
{
    @Autowired
    private NcSchedulingRulesMapper ncSchedulingRulesMapper;

    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询排班规则
     *
     * @param id 排班规则主键
     * @return 排班规则
     */
    @Override
    public NcSchedulingRules selectNcSchedulingRulesById(Long id)
    {
        return ncSchedulingRulesMapper.selectNcSchedulingRulesById(id);
    }

    /**
     * 查询排班规则列表
     *
     * @param ncSchedulingRules 排班规则
     * @return 排班规则
     */
    @Override
    public List<NcSchedulingRules> selectNcSchedulingRulesList(NcSchedulingRules ncSchedulingRules)
    {
        return ncSchedulingRulesMapper.selectNcSchedulingRulesList(ncSchedulingRules);
    }

    /**
     * 新增排班规则
     *
     * @param ncSchedulingRules 排班规则
     * @return 结果
     */
    @Override
    public int insertNcSchedulingRules(NcSchedulingRules ncSchedulingRules)
    {
        ncSchedulingRules.setCreateBy(SecurityUtils.getUsername());
        ncSchedulingRules.setCreateTime(new Date());
        List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
        if(!StringUtils.isNull(ncOrgManages)){
            ncSchedulingRules.setFactoryCode(ncOrgManages.get(0).getJtdm());
            ncSchedulingRules.setFactoryName(ncOrgManages.get(0).getJtmc());
        }
        return ncSchedulingRulesMapper.insertNcSchedulingRules(ncSchedulingRules);
    }

    /**
     * 修改排班规则
     *
     * @param ncSchedulingRules 排班规则
     * @return 结果
     */
    @Override
    public int updateNcSchedulingRules(NcSchedulingRules ncSchedulingRules)
    {
        ncSchedulingRules.setUpdateTime(DateUtils.getNowDate());
        ncSchedulingRules.setUpdateBy(SecurityUtils.getUsername());
        return ncSchedulingRulesMapper.updateNcSchedulingRules(ncSchedulingRules);
    }

    /**
     * 批量删除排班规则
     *
     * @param ids 需要删除的排班规则主键
     * @return 结果
     */
    @Override
    public int deleteNcSchedulingRulesByIds(Long[] ids)
    {
        return ncSchedulingRulesMapper.deleteNcSchedulingRulesByIds(ids);
    }

    /**
     * 删除排班规则信息
     *
     * @param id 排班规则主键
     * @return 结果
     */
    @Override
    public int deleteNcSchedulingRulesById(Long id)
    {
        return ncSchedulingRulesMapper.deleteNcSchedulingRulesById(id);
    }

    @Override
    public String imporIteminfo(List<NcSchedulingRules> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcSchedulingRules org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcSchedulingRules(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、标准 " + org.getName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
